package de.tum.icuproject;

import de.tum.icuproject.MainActivity.SectionsPagerAdapter;
import de.tum.icuproject.model.Player;
import de.tum.icuproject.model.Session;
import de.tum.icuproject.persistence.PlayerRepository;
import de.tum.icuproject.persistence.couchdb.PlayerRepositoryImpl;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * The DeletePlayerFragment asks for confirmation to delete the player and, if
 * confirmed, can do so.
 * 
 */
public class DeletePlayerFragment extends Fragment implements Subscriber {

	private SectionsPagerAdapter sectionsPagerAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.delete_player, container,
				false);

		Button btnConfirmDelete = (Button) rootView
				.findViewById(R.id.btnDeleteConfirmation);
		btnConfirmDelete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				doDelete(v);
			}
		});

		Button btnDeleteReturn = (Button) rootView
				.findViewById(R.id.btnDeleteReturn);
		btnDeleteReturn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				returnToPlayerFragment();
			}
		});

		return rootView;
	}

	/**
	 * Deletes the current player.
	 * 
	 * @param view
	 */
	public void doDelete(View view) {
		PlayerRepository playerRepo = new PlayerRepositoryImpl(this);
		Player player = this.getCurrentPlayer();
		if (player != null) {
			Toast.makeText(getView().getContext(), "Please wait...",
					Toast.LENGTH_SHORT).show();
			playerRepo.delete(player);
		}
	}

	@Override
	public void setCurrentPlayer(Player player) {
	}

	@Override
	public void setPlayerId(int playerId) {
	}

	@Override
	public void confirmInsertion(boolean confirm) {
	}

	@Override
	public void confirmUpdate(boolean confirm) {
	}

	/**
	 * Confirms the deletion of the player or shows an error.
	 * 
	 * @param confirm
	 */
	@Override
	public void confirmDeletion(boolean confirm) {
		Log.d("CONFIRM_DELETION", String.valueOf(confirm));
		if (confirm) {
			Toast.makeText(getActivity(), "Deleted player? = " + confirm,
					Toast.LENGTH_SHORT).show();
			MainActivity mainActivity = (MainActivity) this.getActivity();
			mainActivity.setCurrentPlayer(null);
		} else {
			Toast.makeText(getActivity(), "Error, please try again.",
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void setSessionId(int sessionId) {
	}

	public SectionsPagerAdapter getSectionsPagerAdapter() {
		return sectionsPagerAdapter;
	}

	public void setSectionsPagerAdapter(
			SectionsPagerAdapter sectionsPagerAdapter) {
		this.sectionsPagerAdapter = sectionsPagerAdapter;
	}

	@Override
	public void setCurrentSession(Session session) {
	}

	/**
	 * Returns back to the PlayerFragment.
	 */
	public void returnToPlayerFragment() {
		PlayerFragment playerFragment = new PlayerFragment();
		playerFragment.setSectionsPagerAdapter(sectionsPagerAdapter);
		sectionsPagerAdapter.updatePlayerFragment(playerFragment);
	}

	@Override
	public Player getCurrentPlayer() {
		MainActivity mainActivity = (MainActivity) getActivity();
		return mainActivity.getCurrentPlayer();
	}
}